home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / dnsconf / fqhost.c < prev    next >
C/C++ Source or Header  |  1996-04-12  |  2KB  |  121 lines

  1. #include <string.h>
  2. #include "internal.h"
  3.  
  4.  
  5. PRIVATE void FQHOST::init (const char *host)
  6. {
  7.     const char *pt = host;
  8.     FQHOST_DECOMP *ptd = tb;
  9.     strcpy (ptd->host,"@");
  10.     strcpy (ptd->domain,host);
  11.     ptd++;
  12.     while ((pt = strchr(pt,'.'))!=NULL){
  13.         int len = (int)(pt-host);
  14.         memcpy (ptd->host,host,len);
  15.         ptd->host[len] = '\0';
  16.         strcpy (ptd->domain,pt+1);
  17.         ptd++;
  18.         pt++;
  19.     }
  20.     nbelm = (int)(ptd - tb);
  21. }
  22.  
  23. static void qualify(
  24.     const char *fqhost,
  25.     char *full)
  26. {
  27.     char *pt = strchr(fqhost,'.');
  28.     if (pt != NULL){
  29.         strcpy (full,fqhost);
  30.     }else{
  31.         RESOLV res;
  32.         sprintf (full,"%s.%s",fqhost,res.domain.get());
  33.     }
  34.     int len = strlen(full)-1;
  35.     if (len >= 0 && full[len] == '.') full[len] = '\0';
  36. }
  37.  
  38. /*
  39.     A FQHOST (Fully qualified host) is used to do
  40.     different operation on a hostname, to find out if it
  41.     is a member of a domain, to get its host part (relative
  42.     to the domain etc...
  43. */
  44. PUBLIC FQHOST::FQHOST(const char *host)
  45. {
  46.     char buf[200];
  47.     qualify (host,buf);
  48.     init (buf);
  49. }
  50. PUBLIC FQHOST::FQHOST(const SSTRING &host)
  51. {
  52.     char buf[200];
  53.     qualify (host.get(),buf);
  54.     init (buf);
  55. }
  56.  
  57. PUBLIC FQHOST::FQHOST(const char *host, const char *domain)
  58. {
  59.     char buf[200];
  60.     sprintf (buf,"%s.%s",host,domain);
  61.     init (buf);
  62. }
  63.  
  64. /*
  65.     Return != 0 if the FQHOST is a member of this domain.
  66.     (in fact, if there is a match, it return the number of
  67.      component in the host part).
  68. */
  69. PUBLIC int FQHOST::is_member(
  70.     const char *domain,
  71.     char *hostpart)        // If not NULL, will contain the
  72.                 // host part matching this domain
  73.                 // so that hostpart.domain give
  74.                 // back the fully qualified hostname
  75. {
  76.     int ret = 0;
  77.     FQHOST_DECOMP *ptd = tb;
  78.     for (int i=0; i<nbelm; i++, ptd++){
  79.         /* #Specification: dnsconf / domain / case insensitive
  80.             When trying to match domain name, dnsconf
  81.             use case insensitive string compare.
  82.         */
  83.         if (stricmp(ptd->domain,domain)==0){
  84.             ret = i+1;
  85.             if (hostpart != NULL) strcpy (hostpart,ptd->host);
  86.             break;
  87.         }
  88.     }
  89.     return ret;
  90. }
  91.  
  92. /*
  93.     Get the host part that fit with this domain.
  94. */
  95. PUBLIC const char *FQHOST::gethostpart(const char *domain)
  96. {
  97.     const char *ret = NULL;
  98.     int no = is_member(domain,NULL);
  99.     if (no > 0) ret = tb[no-1].host;
  100.     return NULL;
  101. }
  102.  
  103. /*
  104.     Format the fully qualified (with a trailing dot)
  105. */
  106. PUBLIC void FQHOST::formatfull(char *full)
  107. {
  108.     strcpy (full,tb[0].domain);
  109. }
  110.  
  111. /*
  112.     Format the fully qualified (with a trailing dot)
  113. */
  114. PUBLIC void FQHOST::formatfull(SSTRING &full)
  115. {
  116.     char buf[200];
  117.     formatfull(buf);
  118.     full.setfrom (buf);
  119. }
  120.  
  121.